/*
package com.snap.vseries.fragment.cit;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.snap.vseries.R;
import com.snap.vseries.base.CitFragment;
import com.snap.vseries.bean.TempBean;
import com.snap.vseries.bean.TestItem;
import com.snap.vseries.chart.BarLineChartTouchListenerProxy;
import com.snap.vseries.chart.ELineChart;
import com.snap.vseries.chart.TimeFormatter;
import com.snap.vseries.chart.YAxisTempFormatter;
import com.snap.vseries.dialog.NumberInputDialog;
import com.snap.vseries.framework.MoleExecutor;
import com.snap.vseries.framework.MoleTask;
import com.snap.vseries.serial.Cmd;
import com.snap.vseries.ui.widget.TextButton;
import com.snap.vseries.utils.ToastUtils;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import butterknife.BindView;


@SuppressLint("ValidFragment")
public class TempUpDowmFragment extends CitFragment {

    private static final int DEFAULT_COUNT = 40;
    private static final int DEFAULT_DURATION = 5;

    @BindView(R.id.count_et)
    TextView countEt;

    @BindView(R.id.temp1_tv)
    TextView temp1Tv;
    @BindView(R.id.temp2_tv)
    TextView temp2Tv;
    @BindView(R.id.duration1_tv)
    TextView duration1Tv;
    @BindView(R.id.duration2_tv)
    TextView duration2Tv;

    @BindView(R.id.result_label)
    TextView resultLabel;
    @BindView(R.id.result_tv)
    TextView resultTv;
    @BindView(R.id.start_btn)
    TextButton startBtn;
    @BindView(R.id.chart)
    ELineChart chart;

    private TestItem testItem;
    private final int position;

    private int count = DEFAULT_COUNT;
    private int duration1 = DEFAULT_DURATION;
    private int duration2 = DEFAULT_DURATION;
    private int temp1 = 55;
    private int temp2 = 95;
    private Future future;
    private long cancelClickTime;
    private List<TempBean> tempData = new ArrayList<>();
    private long startTime;

    public TempUpDowmFragment(int position, TestItem testItem) {
        this.position = position;
        this.testItem = testItem;
    }

    @Override
    protected int layoutResId() {
        return R.layout.fragment_cit_temp_up_down;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        initChart();
        countEt.setText(String.valueOf(count));
        resultLabel.setMovementMethod(new ScrollingMovementMethod());
        countEt.setOnClickListener(v ->
                new NumberInputDialog().setParam(0, 1, count, 999, "循环次数", "", value -> {
                    count = (int) value;
                    countEt.setText(String.valueOf(count));
                }).show(getChildFragmentManager(), ""));

        duration1Tv.setText(String.valueOf(duration1));
        duration2Tv.setText(String.valueOf(duration2));
        View.OnClickListener durationListener = v -> {
            int param = 0;
            if (v.getId() == R.id.duration1_tv) {
                param = duration1;
            } else if (v.getId() == R.id.duration2_tv) {
                param = duration2;
            }
            new NumberInputDialog().setParam(0, 1, param, 300, "持续时间", "秒", value -> {
                if (v.getId() == R.id.duration1_tv) {
                    duration1 = (int) value;
                    duration1Tv.setText(String.valueOf(duration1));
                } else if (v.getId() == R.id.duration2_tv) {
                    duration2 = (int) value;
                    duration2Tv.setText(String.valueOf(duration2));
                }
            }).show(getChildFragmentManager(), "");
        };
        duration1Tv.setOnClickListener(durationListener);
        duration2Tv.setOnClickListener(durationListener);

        temp1Tv.setText(String.valueOf(temp1));
        temp2Tv.setText(String.valueOf(temp2));
        View.OnClickListener tempListener = v -> {
            int param = 0;
            if (v.getId() == R.id.temp1_tv) {
                param = temp1;
            } else if (v.getId() == R.id.temp2_tv) {
                param = temp2;
            }
            new NumberInputDialog().setParam(2, 30, param, 100, "温度", "°C", value -> {
                if (v.getId() == R.id.temp1_tv) {
                    temp1 = (int) value;
                    temp1Tv.setText(String.valueOf(temp1));
                } else if (v.getId() == R.id.temp2_tv) {
                    temp2 = (int) value;
                    temp2Tv.setText(String.valueOf(temp2));
                }
            }).show(getChildFragmentManager(), "");
        };
        temp1Tv.setOnClickListener(tempListener);
        temp2Tv.setOnClickListener(tempListener);

        startBtn.setOnClickListener(v -> {
            if (!isTesting()) {
                setTesing(true);
                startBtn.setText("测试中...(双击取消测试)");
                startBtn.setBackgroundResource(R.drawable.btn_cit_running);
                resultLabel.setVisibility(View.GONE);
                resultTv.setText("");
                initChartData();
                startTime = System.currentTimeMillis();
                future = MoleExecutor.getInstance().execute(new MoleTask<Boolean>() {
                    @Override
                    public Boolean runInBackground() throws InterruptedException {
                        try {
                            getSerialPortHelper().open();
                            for (int i = 0; i < count; i++) {
                                if (!keepTemp(temp1, duration1)) return false;
                                if (!keepTemp(temp2, duration2)) return false;
                            }
                            resultTv.post(() -> resultTv.setText("测试通过"));
                            return true;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return false;
                        } finally {
                            try {
                                getSerialPortHelper().execute(Cmd.power(DEVICE_TYPE, 1, false));
                            } catch (Exception e) {

                            }
                            getSerialPortHelper().close();
                        }
                    }

                    @Override
                    public void onCompleted(Boolean result) {
                        if (result) {
                            testItem.setState(1);
                            testItem.setComment("通过");
                        } else {
                            testItem.setState(2);
                            testItem.setComment("失败");
                        }
                        setTesing(false);
                        startBtn.setText("开始测试");
                        startBtn.setBackgroundResource(R.drawable.btn_cit_start);
                        resultLabel.setVisibility(View.VISIBLE);
                        resultLabel.setBackgroundResource(testItem.getState() == 2 ? R.drawable.bg_red_4 : R.drawable.bg_blue6_4);
                        notifyItem(position);
                    }
                });
            } else {
                if ((System.currentTimeMillis() - cancelClickTime) > 2000) {
                    cancelClickTime = System.currentTimeMillis();
                } else {
                    if (future != null) future.cancel(true);
                    ToastUtils.get().showText("已取消！");
                    setTesing(false);
                    startBtn.setText("开始测试");
                    startBtn.setBackgroundResource(R.drawable.btn_cit_start);
//                    resultLabel.setVisibility(View.GONE);
//                    resultTv.setText("");

                    testItem.setState(0);
                    testItem.setComment("");
                    notifyItem(position);
                }
            }
        });
    }

    private boolean keepTemp(int temp, int duration) throws InterruptedException {
        Bundle bundle = getSerialPortHelper().execute(Cmd.setTemp(DEVICE_TYPE, 1, temp * 100));
        if (!bundle.getBoolean("suc")) {
            resultTv.post(() -> resultTv.setText("设置温度失败！"));
            return false;
        }

        int holdTime = 0;
        int holdSecs = 0;
        long tempReachedBeginTime = System.currentTimeMillis();
        while (holdTime < REQUIRE_HOLD_TIME) {
            Thread.sleep(1000);
            if (++holdSecs > 60) {
                resultTv.post(() -> resultTv.setText("无法达到" + temp + "°C,测试失败"));
                return false;
            }
            bundle = getSerialPortHelper().execute(Cmd.readTemp(DEVICE_TYPE, 1));
            if (bundle.getBoolean("suc")) {
                byte[] data = bundle.getByteArray("data");
                if (data != null && data.length > 9) {
                    int tecTemp100 = ((data[8] & 0xff) << 8) + (data[9] & 0xff);
                    float tecTemp = tecTemp100 / 100f;
                    int secX = (int) (System.currentTimeMillis() - startTime) / 1000;
                    tempData.add(new TempBean(tecTemp100, secX));
                    render();
                    if (Math.abs(tecTemp - temp) <= PRECISION) {
                        holdTime++;
                        if (holdTime == 1) tempReachedBeginTime = System.currentTimeMillis();
                    } else if (holdTime > 0) {
                        holdTime = 0;
                    }
                }
            }
        }

        while (System.currentTimeMillis() - tempReachedBeginTime <= (duration - 1) * 1000) {
            Thread.sleep(1000);
        }
        return true;
    }

    private void initChart() {
        BarLineChartTouchListenerProxy proxy =
                new BarLineChartTouchListenerProxy(chart, chart.getViewPortHandler().getMatrixTouch(), 3f);
        chart.setOnTouchListener(proxy);
        //图表整体设置
        chart.setDrawGridBackground(false);//背景颜色
        chart.getDescription().setEnabled(false);//图表的描述信息
        chart.getLegend().setEnabled(false);//图例
        chart.setDrawBorders(false);//图表外边框
        chart.setMaxVisibleValueCount(0);

        //左右的两根Y轴配置
        chart.getAxisRight().setEnabled(false);
        YAxis yAxis = chart.getAxisLeft();//左面的Y轴
        yAxis.setEnabled(true);//开启
        yAxis.setDrawAxisLine(true);//绘制轴线
        yAxis.setDrawGridLines(true);//绘制网格线
        yAxis.enableGridDashedLine(9f, 4f, 0);
        yAxis.setAxisLineColor(0xffe8e9ee);
        yAxis.setGridColor(0xffe8e9ee);
        yAxis.setTextColor(0xff8d909f);
        yAxis.setTextSize(12f);
        yAxis.setValueFormatter(new YAxisTempFormatter());

        //y轴 坐标轴范围 缩放后最大最小坐标轴范围 以及最小分度值 网格线数量
        yAxis.setAxisMinimum(0);
        yAxis.setAxisMaximum(125 * 100);
        chart.setVisibleYRangeMinimum(120, YAxis.AxisDependency.LEFT);//放到最大坐标轴范围显示1.2°C
        yAxis.setGranularity(10f);
        yAxis.setLabelCount(11);

        //X轴配置
        XAxis xAxis = chart.getXAxis();
        xAxis.setEnabled(true);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X轴位置在图表底部
        xAxis.setDrawAxisLine(false);
        xAxis.setDrawGridLines(true);
        xAxis.setGridColor(0xffe8e9ee);
        xAxis.setTextColor(0xff8d909f);
        xAxis.setTextSize(12f);
        xAxis.setValueFormatter(new TimeFormatter());

        //x轴 坐标轴范围 缩放后最大最小坐标轴范围 以及最小分度值 网格线数量
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(8);
        xAxis.setSpaceMax(1);

        //操作相关
        chart.setHighlightPerDragEnabled(false);
        chart.setHighlightPerTapEnabled(false);
        chart.setDoubleTapToZoomEnabled(false);
        chart.setKeepPositionOnRotation(true);
        chart.setTouchEnabled(true);
        chart.setDragEnabled(true);
        chart.setScaleEnabled(true);
        chart.setPinchZoom(true);

        chart.invalidate();
    }

    private void initChartData() {
        tempData.clear();
        if (chart.getData() != null) {
            chart.clearValues();
        }

        List<ILineDataSet> dataSets = new ArrayList<>();
        LineDataSet LidDataSet = new LineDataSet(new ArrayList<>(), "模块温度");
        LidDataSet.setMode(LineDataSet.Mode.LINEAR);
        LidDataSet.setLineWidth(2f);
        LidDataSet.setColor(getResources().getColor(R.color.primary_blue));
        LidDataSet.setDrawCircles(false);
        LidDataSet.setDrawValues(false);
        dataSets.add(LidDataSet);

        LineData lineData = new LineData(dataSets);
        chart.setData(lineData);
        chart.invalidate();
    }

    private void render() {
        resultTv.post(() -> {
            LineData data = chart.getLineData();
            ILineDataSet lidSet = data.getDataSetByIndex(0);
            int lidShowCount = lidSet.getEntryCount();
            int lidDataCount = tempData.size();
            if (lidDataCount > lidShowCount) {
                for (int j = lidShowCount; j < lidDataCount; j++) {
                    lidSet.addEntry(new Entry(tempData.get(j).getSec(), tempData.get(j).getTemperature()));
                }
            }
            if (data.getEntryCount() == 0) return;
            data.notifyDataChanged();
            chart.notifyDataSetChanged();
            chart.setVisibleXRangeMaximum(300);
            chart.moveViewToX(data.getEntryCount());
        });
    }
}*/
